﻿// ActionScript file
/**
 * 加载flv文件类
 * 
 */
package org.nickro.project.widgets{
    import flash.display.DisplayObjectContainer;
    import flash.display.MovieClip;
    import flash.events.Event;
    import flash.events.EventDispatcher;
    import flash.events.NetStatusEvent;
    import flash.events.SecurityErrorEvent;
    import flash.media.Video;
    import flash.net.NetConnection;
    import flash.net.NetStream;
    import flash.utils.Dictionary;
    
    import org.nickro.project.events.VideoEvent;

    public class VideoBox extends MovieClip
    {
        private var videoURL:String = "Video.flv";
        private var connection:NetConnection;
        private var stream:NetStream;
        private var _video:Video;
		private var _parent:DisplayObjectContainer;
		private var videosDict:Dictionary = new Dictionary(true);
		private var videos:Array = [];
		private var _uid:String;
		
		private static var instance:VideoManager;
		
        public function VideoBox() 
        {
			if (stage) init();
			else addEventListener(Event.ADDED_TO_STAGE, init);
        }
		
		private function init(e:Event = null):void
		{
			
		}
		
		public static function getInstance():VideoManager
		{
			if(VideoManager.instance == null)
			{
				VideoManager.instance = new VideoManager(new SingletonEnforcer());
			}
			return VideoManager.instance;
		}
		
		public function addVideo(url:String,parent:DisplayObjectContainer,uid:String):void
		{
			var mc:MovieClip = new MovieClip();
			parent.addChild(mc);
			
			videoURL = url;
			_parent = parent;
			_uid = uid;
			for(var i:int = 0; i < videos.length; i++)
			{
				if(videos[i].uid == uid)
				{
					throw new Error(uid + "has been used!plz change another uid.");
				}
			}
			
			var obj:Object = {};
			obj.video = mc;
			obj.uid = uid;
			videos.push(obj);
			videosDict[uid] = obj;

			connection = new NetConnection();
            connection.addEventListener(NetStatusEvent.NET_STATUS, netStatusHandler);
            connection.addEventListener(SecurityErrorEvent.SECURITY_ERROR, securityErrorHandler);
            connection.connect(null);
		}
		
        private function netStatusHandler(event:NetStatusEvent):void 
        {
            switch (event.info.code) {
                case "NetConnection.Connect.Success":
                	trace("Connection success!")
                    connectStream();
                    break;
                case "NetStream.Play.StreamNotFound":
                    trace("Video" + videoURL +  " is not existed.");
                    break;
            }
        }

        private function securityErrorHandler(event:SecurityErrorEvent):void 
        {
            trace("securityErrorHandler: " + event);
        }

        private function connectStream():void 
        {
            stream = new NetStream(connection);
            stream.addEventListener(NetStatusEvent.NET_STATUS, netStatusHandler);
            stream.client = new Object();
            _video = new Video();         
			_video.attachNetStream(stream);
			stream.play(videoURL);
            videosDict[_uid].video.addChild(_video);
            videosDict[_uid].video.addEventListener(Event.ENTER_FRAME,loadPercent);
        }
        
        private function loadPercent(e:Event):void
        {
        	var percent:int = (int((stream.bytesLoaded/stream.bytesTotal)*100));
        	if(percent == 100)
        	{
        		e.currentTarget.removeEventListener(Event.ENTER_FRAME,loadPercent);
        		dispatchEvent(new VideoEvent(VideoEvent.COMPLETE));
        	}
        	trace(percent);
        }
        
        public function getVideoObject(uid:String):MovieClip
        {
        	//return videosDict[uid].video
        	if(videosDict[uid].video != null)
        	{
        		return videosDict[uid].video
        	}else
        	{
        		throw new Error("video " + uid + "不存在");
        		return null;
        	}
        }
    }
}

class CustomClient{
	public function onMetaData(info:Object):void{
		trace("metadata: duration=" + info.duration + " width=" + info.width + " height=" + info.height + " framerate=" + info.framerate);
	}
	
	public function onCuePoint(info:Object):void{
		trace("cuepoint: time=" + info.time + " name=" + info.name + " type=" + info.type);

	}
}

class SingletonEnforcer {}